package com.domain.component;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
public class RedisUtil {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Resource(name = "stringRedisTemplate")
	ValueOperations<String, String> valOpsStr;

	/**
	 * object 实体类请实现Serializable implements Serializable
	 */
	@Autowired
	RedisTemplate<Object, Object> redisTemplate;

	@Resource(name = "redisTemplate")
	ValueOperations<Object, Object> valOpsObj;

	public Set<String> getkeys() {
		// 输出系统中所有的key
		Set<String> set = this.stringRedisTemplate.keys("*");
		return set;
	}

	/**
	 * 根据指定o获取Object
	 * 
	 * @param o
	 * @return
	 */
	public Object getObj(Object o) {
		return valOpsObj.get(o);
	}

	/**
	 * 设置obj缓存
	 * 
	 * @param o1
	 * @param o2
	 */
	public boolean setObj(Object o1, Object o2) {
		boolean result = false;
		try {
			valOpsObj.set(o1, o2);
			result = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 设置obj缓存
	 * 
	 * @param o1
	 * @param o2
	 * @param expireTime
	 */
	public boolean setObj(Object o1, Object o2, Long expireTime) {
		boolean result = false;
		try {
			valOpsObj.set(o1, o2);
			result = redisTemplate.expire(o1, expireTime, TimeUnit.SECONDS);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 删除Obj缓存
	 * 
	 * @param o
	 */
	public void delObj(Object o) {
		redisTemplate.delete(o);
	}

	/**
	 * 批量删除对应的value
	 * 
	 * @param keys
	 */
	public void remove(final String... keys) {
		for (String key : keys) {
			remove(key);
		}
	}

	/**
	 * 批量删除key
	 * 
	 * @param pattern
	 */
	public void removePattern(final String pattern) {
		Set<String> keys = stringRedisTemplate.keys(pattern);
		if (keys.size() > 0)
			stringRedisTemplate.delete(keys);
	}

	/**
	 * 删除对应的value
	 * 
	 * @param key
	 */
	public void remove(final String key) {
		if (exists(key)) {
			stringRedisTemplate.delete(key);
		}
	}

	/**
	 * 判断缓存中是否有对应的value
	 * 
	 * @param key
	 * @return
	 */
	public boolean exists(final String key) {
		return stringRedisTemplate.hasKey(key);
	}

	/**
	 * 读取缓存
	 * 
	 * @param key
	 * @return
	 */
	public String get(final String key) {
		String result = null;
		result = valOpsStr.get(key);
		return result;
	}

	/**
	 * 读取redis存时间
	 * 
	 * @param key
	 * @return
	 */
	public long getTime(final String key) {
		long result = 0;
		result = stringRedisTemplate.getExpire(key);
		return result;
	}

	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, String value) {
		boolean result = false;
		try {
			valOpsStr.set(key, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 写入缓存
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, String value, Long expireTime) {
		boolean result = false;
		try {
			valOpsStr.set(key, value);
			stringRedisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 更新缓存(value--为空时表删除，有值时更新)
	 * 
	 * @param paramap
	 * @param expireTime
	 * @return
	 */
	public boolean update(String key, String value, Long expireTime) {
		boolean result = false;
		if (key == null || key == "") {
			return result;
		}
		if (value == null || value == "") {
			remove(key);
			return true;
		}
		String vaString = get(key);
		if (vaString != null) {
			remove(key);
		}
		if (expireTime == null) {
			result = set(key, value);
			return result;
		}
		result = set(key, value, expireTime);
		return result;
	}
}
